package com.daikit.daikit4gxt.client.ui.editor.field;

import com.daikit.commons.shared.bean.DkTime;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.ui.editor.advanced.AbstractDkHideableAdapterField;
import com.daikit.daikit4gxt.client.ui.editor.field.validator.DkMinMaxFixedNumberValidator;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer.HorizontalLayoutData;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;


/**
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkTimeField extends AbstractDkHideableAdapterField<Integer> implements HasValueChangeHandlers<Integer>
{

	private final NumberField<Integer> hour;
	private final NumberField<Integer> minute;
	private final NumberField<Integer> second;

	private final FieldLabel hourFieldLabel;
	private final FieldLabel minuteFieldLabel;
	private final FieldLabel secondFieldLabel;

	private boolean enabled = true;

	private boolean showSeconds = true;

	private final int height = 26;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// CONSTRUCTOR
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Constructor with showSeconds = true
	 */
	public DkTimeField()
	{
		this(true);
	}


	/**
	 * Constructor
	 *
	 * @param showSeconds
	 */
	public DkTimeField(final boolean showSeconds)
	{
		super(new HorizontalLayoutContainer());
		this.showSeconds = showSeconds;
		final HorizontalLayoutContainer panel = (HorizontalLayoutContainer) getWidget();

		hour = new NumberField<Integer>(new NumberPropertyEditor.IntegerPropertyEditor());
		minute = new NumberField<Integer>(new NumberPropertyEditor.IntegerPropertyEditor());
		second = new NumberField<Integer>(new NumberPropertyEditor.IntegerPropertyEditor());

		hourFieldLabel = new FieldLabel(hour, getHourFieldLabelValue());
		minuteFieldLabel = new FieldLabel(minute, getMinuteFieldLabelValue());
		secondFieldLabel = new FieldLabel(second, getSecondFieldLabelValue());

		setLabelsWidth(70);
		setHeight(height);

		panel.add(hourFieldLabel, new HorizontalLayoutData(showSeconds ? 0.33 : 0.5, 1, new Margins(0, 5, 0, 0)));
		panel.add(minuteFieldLabel, new HorizontalLayoutData(showSeconds ? 0.33 : 0.5, 1, new Margins(0, showSeconds ? 5 : 0, 0, 0)));
		if (showSeconds)
		{
			panel.add(secondFieldLabel, new HorizontalLayoutData(0.33, 1, new Margins(0, 0, 0, 0)));
		}

		hour.addValidator(new DkMinMaxFixedNumberValidator<Integer>(0, 23, false, false, false));
		minute.addValidator(new DkMinMaxFixedNumberValidator<Integer>(0, 59, false, false, false));
		if (showSeconds)
		{
			second.addValidator(new DkMinMaxFixedNumberValidator<Integer>(0, 59, false, false, false));
		}
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	@Override
	public void setValue(final Integer value)
	{
		final DkTime time = new DkTime(value, !showSeconds);
		hour.setValue(time.getHours());
		minute.setValue(time.getMinutes());
		second.setValue(time.getSeconds());
	}

	@Override
	public Integer getValue()
	{
		return new DkTime(hour.getValue(), minute.getValue(), showSeconds ? second.getValue() : 0).getTime();
	}

	/**
	 * Set field labels width (default is 80)
	 *
	 * @param width
	 *           the width to set
	 */
	public void setLabelsWidth(final int width)
	{
		hourFieldLabel.setLabelWidth(width);
		minuteFieldLabel.setLabelWidth(width);
		secondFieldLabel.setLabelWidth(width);
	}

	@Override
	public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<Integer> handler)
	{
		return addHandler(handler, ValueChangeEvent.getType());
	}

	@Override
	public void setEnabled(final boolean enabled)
	{
		hour.setEnabled(enabled);
		minute.setEnabled(enabled);
		second.setEnabled(enabled);
		this.enabled = enabled;
	}

	@Override
	public boolean isEnabled()
	{
		return enabled;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	protected String getHourFieldLabelValue()
	{
		return DkBaseMain.i18n().timeField_hour_label();
	}

	protected String getMinuteFieldLabelValue()
	{
		return DkBaseMain.i18n().timeField_minute_label();
	}

	protected String getSecondFieldLabelValue()
	{
		return DkBaseMain.i18n().timeField_second_label();
	}

	/**
	 * @return the hour field
	 */
	public NumberField<Integer> getHourField()
	{
		return hour;
	}

	/**
	 * @return the minute field
	 */
	public NumberField<Integer> getMinuteField()
	{
		return minute;
	}

	/**
	 * @return the second field
	 */
	public NumberField<Integer> getSecondField()
	{
		return second;
	}

	/**
	 * @return the showSeconds
	 */
	public boolean isShowSeconds()
	{
		return showSeconds;
	}

	/**
	 * @return the hourFieldLabel
	 */
	public FieldLabel getHourFieldLabel()
	{
		return hourFieldLabel;
	}

	/**
	 * @return the minuteFieldLabel
	 */
	public FieldLabel getMinuteFieldLabel()
	{
		return minuteFieldLabel;
	}

	/**
	 * @return the secondFieldLabel
	 */
	public FieldLabel getSecondFieldLabel()
	{
		return secondFieldLabel;
	}
}
